"""
This Python script will perform an incoherent scan which duplicates the
motor positions for a normal scan.  It takes care of handling whether you're
doing a fixed Ei or fixed Ef scan and holds the appropriate motors.
"""

import sys, string
import PySpice

"""
Read in the energy mode and fixed_energy variables
"""

emode=PySpice.getvar('emode')
energy=PySpice.getvar('fixed_energy')

if (emode == 0) or (emode == 2):
    ei_fixed=1
else:
    ei_fixed=0

"""
Drive either ei or ef to energy and then
hold either a2 and a1 or m2 depending on
fixed ei/ef
"""

if ei_fixed:
    print "Driving Ef to " + str(energy) + " and holding a2 and a1"
    PySpice.Command('logfileoutput off')
    PySpice.Command('drive ef ' + str(energy))
    PySpice.Command('hold a2 1 a1 1')
else:
    print "Driving Ei to " + str(energy) + " and holding m2"
    PySpice.Command('logfileoutput off')
    PySpice.Command('drive ei ' + str(energy))
    PySpice.Command('hold m2 1')

PySpice.Command('logfileoutput on')

"""
Execute the given scan
"""

PySpice.Command('showdest 1')
PySpice.Command('scan ' + string.join(sys.argv[1:]))

print "Finished incoherent scan - removing holds"

PySpice.Command('logfileoutput off')

"""
Reset the holds and the showdest commant
"""

if ei_fixed:
    PySpice.Command('hold a2 0 a1 0')
else:
    PySpice.Command('hold m2 0')

PySpice.Command('showdest 0')

PySpice.Command('logfileoutput on')
